package com.example.minefieldkeepercloud;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import com.example.minefieldkeepercloud.bean.LoginBean;
import com.example.minefieldkeepercloud.interfac.ApiHttpCallback;
import com.example.minefieldkeepercloud.network.MyGson;
import com.example.minefieldkeepercloud.network.NetHelperNew;
import com.example.minefieldkeepercloud.util.NetWorkUtil;
import com.example.minefieldkeepercloud.util.SPUtilHelper;
import com.example.minefieldkeepercloud.util.ToastUtil;

import butterknife.BindString;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 登录页
 */
public class LoginAc extends AppCompatActivity {
    @BindView(R.id.edit_id)
    EditText edit_id;

    @BindView(R.id.edit_pwd)
    EditText edit_pwd;

    @BindString(R.string.internet_error)
    String InterNetError;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_login);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        Intent intent = getIntent();
        if (!isTaskRoot()
                && intent != null
                && intent.hasCategory(Intent.CATEGORY_LAUNCHER)
                && intent.getAction() != null
                && intent.getAction().equals(Intent.ACTION_MAIN)) {
            finish();
            return;
        }


        edit_id.setText(SPUtilHelper.getPhone());
        edit_pwd.setText(SPUtilHelper.getPwd());
    }

    @OnClick({R.id.btn_login})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_login:
                Login();
                break;
        }
    }

    /**
     * 登录
     */
    private void Login() {
        if (NetWorkUtil.check(this)) {
            final String id = edit_id.getText().toString();
            final String pwd = edit_pwd.getText().toString();
            if (id.isEmpty() || pwd.isEmpty()) {
                ToastUtil.showToast(this, "请填写账号密码!!!!!");
                return;
            }

            NetHelperNew.login(id, pwd, new ApiHttpCallback(this, 1, true) {
                @Override
                public void onSuccess(String data) {
                    Log.i("-->", "登录返回：" + data);
                    LoginBean loginBean = MyGson.getInstance().fromJson(data, LoginBean.class);
                    if (loginBean.getType() == 1) {

                        SPUtilHelper.setId(loginBean.getData().getID());
                        SPUtilHelper.setName(loginBean.getData().getFullName());
                        SPUtilHelper.setPhoneNo(loginBean.getData().getMobilePhone());
                        SPUtilHelper.setPhone(id);
                        SPUtilHelper.setPwd(pwd);

                        startActivity(new Intent(LoginAc.this, NewMainActivity.class));
                        finish();
                    } else {
                        ToastUtil.showToast(LoginAc.this, loginBean.getContent());
                    }

                }


            });
        }

    }


}
